package com.atguigu.yygh.hosp.controller;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/12 22:50
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

  @Autowired
  private HospitalSetService hospitalSetService;

  // 医院的锁定与解锁 1 解锁 0 锁定
  @ApiOperation(value = "医院的锁定与解锁 1 解锁 0 锁定")
  @PutMapping("lockHospitalSet/{id}/{status}")
  public Result lockHospitalSet(@PathVariable Long id,
                                @PathVariable Integer status){
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    // 是否锁定 1 未锁定 0 锁定
    hospitalSet.setStatus(status);
    hospitalSetService.updateById(hospitalSet);
    return Result.ok();
  }

  // 发送密钥
  @ApiOperation(value = "发送密钥")
  @PutMapping("sendKey/{id}")
  public Result sendKey(@PathVariable Long id){
    HospitalSet send = hospitalSetService.getById(id);
    String signKey = send.getSignKey();
    String hosname = send.getHosname();
    String hoscode = send.getHoscode();
    // TODO 发送短信
    return Result.ok();
  }


  // 添加医院的设置
  @ApiOperation(value = "添加医院的设置")
  @PostMapping("saveHospitalSet")
  public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
    // 设置状态 1 使用 0 不能使用
    hospitalSet.setStatus(1);
    // 签名密钥
    Random random = new Random();
    hospitalSet.setSignKey(MD5.encrypt(System
        .currentTimeMillis()+""+random.nextInt(1000)));
    // 调用service
    boolean flag = hospitalSetService.save(hospitalSet);
    if (flag){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  // 批量删除医院的设置
  @ApiOperation(value = "批量删除医院的设置")
  @DeleteMapping("batchRemove")
  public Result batchRemove(@RequestBody List<Long> ids){
    boolean flag = hospitalSetService.removeByIds(ids);
    if (flag){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

//修改医院设置
  @ApiOperation(value = "修改医院设置")
  @PostMapping("updateHospitalSet")
  public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
    boolean flag = hospitalSetService.updateById(hospitalSet);
    if (flag){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  // 根据id查找医院
  @ApiOperation(value = "根据id查找医院")
  @GetMapping("getHospSet/{id}")
  public Result getHospSet(@PathVariable Long id){
    HospitalSet hospitalSet = hospitalSetService.getById(id);
    return Result.ok(hospitalSet);
  }

  // 分页查询
  @ApiOperation(value = "根据条件(医院名称 医院编号)分页查询")
  // @RequestBody :使用json数据来传输对象 必须使用post方式,get方式不生效
  @PostMapping("findPageHospSet/{current}/{limit}")
  public Result findPage(@PathVariable long current,
                         @PathVariable long limit,
                         @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
    // 创建page对象 ,传递参数 ,每页记录数
    Page<HospitalSet> page = new Page<>(current,limit);
    // 构建查询对象
    QueryWrapper<HospitalSet> wrapper = new QueryWrapper<HospitalSet>();

    // 查看条件是否为空
    if (hospitalSetQueryVo != null){
      String hoscode = hospitalSetQueryVo.getHoscode(); // 医院编号
      String hosname = hospitalSetQueryVo.getHosname(); // 医院名称
      if (!StringUtils.isEmpty(hoscode)){
        wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
      }
      if (!StringUtils.isEmpty(hosname)){
        wrapper.like("hosname", hospitalSetQueryVo.getHosname());
      }
    }
    // 查询结果
    Page<HospitalSet> setPage = hospitalSetService.page(page, wrapper);
    return Result.ok(setPage);
  }

  // 查询医院设置表所有信息
  @ApiOperation(value = "获取医院设置列表")
  @GetMapping("findAll")
  public Result findAllHospitalSet(){
    return Result.ok(hospitalSetService.list());
  }

  // 逻辑删除医院设置
  @ApiOperation(value = "逻辑删除医院设置")
  @DeleteMapping("{id}")
  public Result removeHospSet(@PathVariable("id") long id){
    boolean flag = hospitalSetService.removeById(id);
    if (flag){
      return Result.ok();
    }else {
      return Result.fail();
    }


  }
}
